package people;

import java.net.InetAddress;
import java.util.ArrayList;

/**
 * Package  people
 * Project ChatSystemG2 - INSA 4INFOA1 - 2013/2014
 * 
 * UsersList contains the list of the connected users
 *
 * @author Sergio Gonzalez - Marie-Charlotte Bideaud
 */

public class UsersList {

	// List of the connected users 
	private ArrayList<User> userslist ;

	/**
	 * Constructor
	 */
	public UsersList (){
		this.userslist = new ArrayList<User>();
	}

	/**
	 * Getter of userslist
	 * @return userslist
	 */

	public ArrayList<User> getUserslist() {
		return userslist;
	}

	/** 
	 * Setter of userslist
	 * @param userslist
	 */

	public void setUserslist(ArrayList<User> userslist) {
		this.userslist = userslist;
	}

	/**
	 * Add a user to the userslist
	 * @param user 
	 */
	
	public void addUser(User user) {
		this.userslist.add(user);
	}

	/**
	 * Remove a user from the userslist
	 * @param user : User 
	 */
	
	public void removeUser(User user) {
		for(User u : userslist){
			if ( (u.getUsername().equals(user.getUsername())) 
					&& (u.getIPAddr().equals(user.getIPAddr()))) {
				userslist.remove(u);
				break;
			}
		}
	}
	
	/**
	 * Remove all users from the userslist
	 */
	
	public void removeAllUser() {
		userslist.clear();
	}

	/**
	 * Return a user from its IP address
	 * @param addr 
	 * @return the user corresponding to addr
	 */
	
	public User getUser(InetAddress addr) {
		User user = null;
		for(User u : userslist){
			if(u.getIPAddr().equals(addr)) {
				user = u;
				break;
			}
		}
		return user;
	}

}
